/**
 * formats [ normal_format, title_format, 
 * 
 * 
 * 
 * 
 * ***/
package Avocado.visual {
	import Avocado.util.MoveObject;
	import Avocado.util.StartDragPerspective;
	
	import com.greensock.TweenMax;
	
	import fl.transitions.Tween;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.geom.Rectangle;
	import flash.text.AntiAliasType;
	import flash.text.Font;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	import mx.core.EventPriority;
	
	
	/**
	 * classe de criacao de blocos de texto
	 * ***/
	public class TextBox extends MovieClip {
		
		public static const LOREM_IPSUM:String = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ' +
			'Donec ac leo ac nibh tristique sodales. Vestibulum volutpat metus ut ligula adipiscing nec eleifend enim placerat. ' +
			'Etiam arcu nisi, dictum vel dictum tristique, elementum id odio. Maecenas non porta purus. Cras consectetur hendrerit' +
			' quam ut vehicula. Quisque convallis suscipit rhoncus. Quisque aliquam, sem quis dapibus bibendum, lorem nibh fermentum ' +
			'erat, a aliquam neque nunc posuere arcu. Maecenas vel odio a mi facilisis congue non in arcu. Mauris enim erat, gravida ' +
			'nec viverra et, posuere eget eros. Maecenas vel diam neque, eget pellentesque quam. Morbi facilisis purus eget enim adipiscing ' +
			'sollicitudin. Suspendisse suscipit, sapien ac mollis ullamcorper, magna justo dignissim massa, vitae pellentesque dolor orci eu nisl. ' +
			'Etiam orci eros, fermentum eget tempus id, mollis sit amet dolor. Nulla vel ante neque, sed varius libero. Cras quis massa ' +
			'sodales ante lobortis condimentum vitae quis magna. Morbi vel massa metus. Suspendisse tellus dolor, condimentum a lobortis ' +
			'non, egestas nec elit. Nulla justo velit, placerat vitae varius in, elementum vel ligula. Mauris semper lobortis lacinia. Sed ' +
			'faucibus faucibus auctor. Nam euismod auctor massa, faucibus feugiat sapien tempus vitae. Etiam vel massa sit amet eros ' +
			'sollicitudin rutrum. Mauris bibendum laoreet odio, ut pellentesque nisl consectetur nec. Nullam sem enim, pulvinar non dignissim ' +
			'at, interdum eu ante. Morbi aliquam neque vel erat scelerisque pulvinar. Proin non ligula libero. Aliquam nunc nisl, tincidunt id ' +
			'adipiscing nec, ultricies vel turpis. Nunc egestas molestie dui vel ultricies.';
		
		public static const HEIGHT_UPDATED:String = new String('heightUpdated');
		
		private var text:String;
		private var formats:Array;
		private var txWidth:Number;
		private var txHeight:Number;
		private var renderHTML:Boolean;
		private var autoHeight:Boolean;
		private var cssText:String;
		
		private var _barColor:uint = 0x9b9b9b;
		private var _bgBarColor:uint = 0x707070;
		
		public var field:TextField;
		
		private var tMask:Sprite;
		private var bgBar:Sprite;
		private var theBar:Sprite;
		private var isDragBar:Boolean = new Boolean(false);
		private var style:StyleSheet;
		
		public function TextBox(_formats:Array, _text:String='text here!', _width:Number=360, _height:Number=294, _renderHTML:Boolean=true, _autoHeight:Boolean=true, _cssText:String=null) {
			super();
			
			this.text = _text;
			this.formats = _formats;
			this.txWidth = _width;
			this.txHeight = _height;
			this.renderHTML = _renderHTML;
			this.autoHeight = _autoHeight;
			this.cssText = _cssText;
			
			this.addEventListener(Event.ADDED_TO_STAGE, initThis);
		}

		

		/**
		 * setup the bar color
		 * @color : uint
		 * ***/
		public function set barColor(color:uint):void{
			if (color)
				_barColor = color;
		}
		public function get barColor():uint{
			return _barColor;
		}



		/**
		 * setup the background color
		 * @color : uint
		 * ***/
		public function set bgBarColor(color:uint):void{
			if (color)
				_bgBarColor = color;
		}
		public function get bgBarColor():uint{
			return _bgBarColor;
		}
		
		
		

		
		
		/**
		 * inicialização
		 * ***/
		private function initThis(e:Event):void{
			this.removeEventListener(Event.ADDED_TO_STAGE, initThis);
			setupText();
		}


		/**
		 * buildding the text
		 * ***/
		private function setupText():void{
			field = new TextField;
			if (renderHTML == true) {
				field.htmlText = this.text;
			} else {
				field.text = this.text;	
			}

			field.width = this.txWidth;
			
			if (autoHeight == true){
				updateTextHeight();
			} else {
				field.height = this.txHeight;
			}
			 
			field.multiline = true;
			field.wordWrap = true;
			field.autoSize = TextFieldAutoSize.LEFT;
			field.defaultTextFormat = this.formats[0];
			field.setTextFormat(this.formats[0]);
			field.selectable = false;
			field.embedFonts = true;
			field.mouseEnabled = renderHTML;
			field.mouseWheelEnabled = false;
			addChild(field);
			
			if (this.formats[1]){
				findTitle();
			}
			
			if (this.cssText){
				parseCSS();
			}
		}
			private function updateTextHeight():void{
				var heightToUpdate:Number = new Number(field.textHeight + int(this.formats[0].size));
				if (heightToUpdate > txHeight) {
					field.height = heightToUpdate;	
				} else {
					field.height = txHeight;
				}
				
				this.dispatchEvent(new Event(HEIGHT_UPDATED));
			}
			private function findTitle():void{
				var titleRule:RegExp = new RegExp(/===(.)*?\===/gsm);
				var titles:Array = new Array;
				if (this.text.toString().search(titleRule) > 0) {
					titles = this.text.toString().match(titleRule);
				}
				field.text = this.text.split('===').join('');
				
				var ct:int = 0;
				for each (var title:* in titles){
					var start:Number = this.text.toString().search(title) - (6*ct); 
					var end:Number = start + title.length - 6;
					field.setTextFormat(this.formats[1], start, end);
					ct++;
				}
			}

			private function parseCSS():void{
				if (this.cssText) {
					style = new StyleSheet;
					style.parseCSS(this.cssText);
					
					field.styleSheet = style;
				}
			}

	}
}